package ar.edu.unq.wannaplay.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

import ar.edu.unq.wannaplay.exceptions.BlankStringException;
import ar.edu.unq.wannaplay.exceptions.NullArgumentException;
import ar.edu.unq.wannaplay.exceptions.UserAlreadyExistsException;
import ar.edu.unq.wannaplay.services.UserService;
import ar.edu.unq.wannaplay.users.Role;

public class RegisterPanel extends ServicedPanel<UserService> {

    private static final long serialVersionUID = 1L;

    private FeedBackPanel feedback;

    private String username;

    private String password;

    private Role role;

    public RegisterPanel(final String id, final UserService service, final FeedBackPanel feedback) {
        super(id, service);
        this.setFeedback(feedback);
        this.initialize();
    }

    private void initialize() {
        CompoundPropertyModel<RegisterPanel> loginModel = new CompoundPropertyModel<RegisterPanel>(this);
        Form form = new Form("form", loginModel);
        form.add(new TextField("username"));
        form.add(new DropDownChoice<Role>("role", this.getService().findAllRoles()));
        form.add(new PasswordTextField("password"));
        form.add(this.createRegisterButton("register"));
        this.add(form);
    }

    private AjaxButton createRegisterButton(final String id) {
        return new AjaxButton(id) {
            private static final long serialVersionUID = 1L;

            @SuppressWarnings("synthetic-access")
            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                try {
                    RegisterPanel.this.getService().register(RegisterPanel.this.getUsername(),
                            RegisterPanel.this.getPassword(), RegisterPanel.this.getRole());
                    RegisterPanel.this.getFeedback().changeMessage(
                            this.getLocalizer().getString("register.succes", this));
                } catch (UserAlreadyExistsException e) {
                    RegisterPanel.this.getFeedback().changeMessage(
                            this.getLocalizer().getString("register.userAlreadyExists", this));
                } catch (BlankStringException e) {
                    RegisterPanel.this.getFeedback().changeMessage(
                            this.getLocalizer().getString("register.blackString", this));
                } catch (NullArgumentException e) {
                    RegisterPanel.this.getFeedback().changeMessage(
                            this.getLocalizer().getString("register.nullCombo", this));

                } finally {
                    target.addComponent(RegisterPanel.this.getFeedback());
                }

            }
        };
    }

    public Role getRole() {
        return role;
    }

    public void setFeedback(final FeedBackPanel feedback) {
        this.feedback = feedback;
    }

    public FeedBackPanel getFeedback() {
        return feedback;
    }

    public void setUsername(final String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setRole(final Role role) {
        this.role = role;
    }

}
